<?php

// application/controllers/TblcompanyController.php

class TblcompanyController extends Zend_Controller_Action {

    private $logger;

    public function init() {
        $this->view->headTitle('BBViande - Company');
        //$this->view->headTitle('BBViande - Adresse');
        $this->logger = Zend_Registry::get('logger');
        $this->logger->info("Hello from TblcompanyController");
    }

    public function indexAction() {
        $tblcompany = new Application_Model_TblcompanyMapper();
        $this->view->entries = $tblcompany->fetchAll();
    }

    public function deleteAction() {
        $this->view->delete_id = $this->getRequest()->getParam('delete_id');
        $mapper = new Application_Model_TblcompanyMapper();
        $mapper->deleteById($this->view->delete_id);

        return $this->_helper->redirector('index');
    }

    public function signAction() {
        $request = $this->getRequest();
        $form = new Application_Form_Tblcompany();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $allValue = new Application_Model_Tblcompany($form->getValues());
                $mapper = new Application_Model_TblcompanyMapper();
                $mapper->save($allValue);
                return $this->_helper->redirector('index');
            }
        }

        $this->view->form = $form;
    }

    public function editAction() {
        //$this->logger->info("editAction");
        $form = new Application_Form_Tblcompany ( );
        $form->setIdCompany($this->getRequest()->getParam('id'));
        $form->init();
        $this->view->formUserEdit = $form;

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $company = new Application_Model_DbTable_Tblcompany ( );
                unset($formData ['submit']);
                $this->logger->info(Zend_Debug::dump($formData, $label = null, $echo = true));

                $where = $company->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id'));               
                $result = $company->update($formData, $where);

                if ($result) {
                    $this->_helper->redirector('index', 'Tblcompany', '', array('' => ''));
                } else {
                    $this->_helper->redirector('index', 'Tblcompany', '', array('' => ''));
                }
            }
        }
    }

}

